<template>
  <el-dialog v-model="dialogVisible" title="修改密码" width="500px" draggable>
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="原密码" prop="oldPasswd">
        <el-input placeholder="请输入原密码" type="password" v-model="form.oldPasswd" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPasswd">
        <el-input placeholder="请输入新密码" type="password" v-model="form.newPasswd" show-password></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="_submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { modifyPsdApi } from "@/api/modules/login";
const dialogVisible = ref(false);
const form = reactive({ oldPasswd: "", newPasswd: "" });
const formRef = ref<FormInstance>();
const rules = reactive({
  oldPasswd: [
    {
      required: true,
      message: "原密码不能为空",
      trigger: "blur"
    },
    {
      //插入正则验证：大小写、数字、至少6位、不常用字符
      pattern: /^((?=.*[a-zA-Z])(?=.*\d))*((?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+]))*[a-zA-Z\d!@#$%^&*()_+]{6,18}$/,
      message: "密码6-18位(含数字、字母、特殊字符_-)",
      trigger: "blur"
    }
  ],
  newPasswd: [
    {
      required: true,
      message: "新密码不能为空",
      trigger: "blur"
    },
    {
      //插入正则验证：大小写、数字、至少6位、不常用字符
      pattern: /^((?=.*[a-zA-Z])(?=.*\d))*((?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+]))*[a-zA-Z\d!@#$%^&*()_+]{6,18}$/,
      message: "密码6-18位(含数字、字母、特殊字符_-)",
      trigger: "blur"
    }
  ]
});
const openDialog = () => {
  dialogVisible.value = true;
};
/**
 * 提交修改信息
 */
const _submit = async (): Promise<void> => {
  await modifyPsdApi(form);
  ElMessage({
    message: "修改成功",
    type: "success"
  });
  dialogVisible.value = false;
};
defineExpose({ openDialog });
</script>
